function StartConsumer(accept) {
    if (!accept) {
        throw new Error("accept cannot be null");
    }
    if (typeof accept !== 'function') {
        throw new Error("accept must be a function");
    }

    // 检查是否通过 new 调用
    if (!(this instanceof StartConsumer)) {
        return new StartConsumer(accept);
    }

    this.accept = accept;

    function andThen(after) {
        if (after == null) {
            return this; // 返回当前实例，而不是 null
        }
        if (!(after instanceof StartConsumer)) {
            throw new Error("after must be an instance of StartConsumer");
        }

        const that = this;
        const callBack = function (...args) {
            that.accept.apply(that, args); // 调用当前的 accept 方法
            after.accept.apply(after, args); // 调用 after 的 accept 方法
        };

        return new StartConsumer(callBack);
    }

    function compose(before) {
        if (before == null) {
            return this; // 返回当前实例，而不是 null
        }
        if (!(before instanceof StartConsumer)) {
            throw new Error("before must be an instance of StartConsumer");
        }

        const that = this;
        const callBack = function (...args) {
            before.accept.apply(before, args); // 调用 before 的 accept 方法
            that.accept.apply(that, args); // 调用当前的 accept 方法
        };
        return new StartConsumer(callBack);
    }

    this.andThen = andThen;
    this.compose = compose;
}

export default StartConsumer;